package com.younge.spring.api.common;

import java.util.Arrays;

/**
 * 响应码枚举类
 */
public enum ResultCodeEnum {

    SUCCESS(0, "操作成功"),
    FORBIDDEN(401, "未登录"),
    UNAUTHORIZED(403, "没有相关权限"),
    VALIDATE_FAILED(404, "参数检验失败"),
    FIELD(500, "操作失败");

    // 响应码
    private Integer code;
    //响应信息
    private String message;

    ResultCodeEnum(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 根据响应码获取枚举
     */
    public static ResultCodeEnum fromCode(String code) {
        return Arrays.stream(values()).filter(v -> String.valueOf(v.getCode()).equals(code)).findFirst().orElse(null);
    }

    /**
     * 根据枚举响应信息获取枚举
     */
    public static ResultCodeEnum fromDesc(String message) {
        return Arrays.stream(values()).filter(v -> v.getMessage().equals(message)).findFirst().orElse(null);
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}
